import { computed, ref, watch, reactive } from 'vue'
import { apiInit } from '@/services'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'

export function useAgreement() {
	const store = useStore()
	const route = useRoute()
	const showAgreementDialog = ref(false)
	const agreement = ref([] as typing.UserAgreementBo[])
	const tenantName = computed(() => store?.state?.tenant?.tenant_name)
	const tenantLogo = computed(() => store?.state?.tenant?.blue_logo_url)
	//是否展示新版本提示
	const showNewVersion = ref(false)
	// 判断是否是东信
	const isThirdApp = computed(() => {
		const third_app_shop_url: string = store.state.tenant.third_app_shop_url
		const res = third_app_shop_url == '' || !third_app_shop_url ? false : true
		return res
	})
	//视频介绍状态
	const videoFlag = ref(false)
	//密码修改提示
	const editPasswordStatus = ref(false)
	const userInfo: any = reactive({
		user_id: '',
		password: '',
		phone: '',
	})
	const showNewUserRead = async () => {
		//○ 租户管理平台，新用户引导开关=开启
		//○ 访问的用户拥有【设计态访问权限】
		//○ 该用户新用户引导状态=【未读】
		const r = await apiInit().baseAuto.ListGuideUserPrivateV1POST({
			payload: ['portal1', 'new_user_guide', 'org_pwd_has_set'],
		})
		// r.data && (r.data[2].status = false)
		r.data && (store.state.org_pwd_has_set = r.data[2].status)
		//公共逻辑  租户管理平台开关打开&&有设计态权限
		const commonAuth =
			store.state.tenant?.tenant_auth?.portal_new_user_guide &&
			store.state.userInfo?.app_admin
		//是否展示新版本
		showNewVersion.value =
			r?.data?.length && !r?.data[1]?.status && !isThirdApp.value && commonAuth
				? true
				: false
		// 1的时候等待新手引导完成会触发监听调用
		if (showNewVersion.value) store.state.showPwdType = 1
		else if (r.data && !r.data[2].status) store.state.showPwdType = 2
		videoFlag.value =
			r?.data?.length &&
			!r?.data[1]?.status &&
			store.state.tenant?.create_time >= +new Date('2022/03/11 20:00') &&
			commonAuth
				? true
				: false
		// showNewVersion.value =true
		// videoFlag.value =true
	}
	apiInit()
		.default.QueryNoSignedAgreeUserPrivateV2POST({})
		.then(result => {
			agreement.value = result.data ?? []
			if (agreement.value.length > 0) {
				if (route.path === '/welcome') {
					apiInit().default.SaveAgreeUserPrivateV2POST({
						payload: agreement.value,
					})
				} else {
					showAgreementDialog.value = true
				}
			} else {
				//是否展示视频
				showNewUserRead()
			}
		})
	const agreementClick = (event: any) => {
		const result = event.detail[0]
		if (result === 'agree') {
			apiInit().default.SaveAgreeUserPrivateV2POST({
				payload: agreement.value,
			})
			showAgreementDialog.value = false
			//是否展示视频
			showNewUserRead()
		} else {
			window.close()
			window.location.href = process.env.VUE_APP_LOGOUT ?? ''
		}
	}
	//新版本提示结束
	const newVersionEnd = computed(() => store.state.showNewVersionTipsEnd)
	//用户指引结束
	const showUserGuideEnd = computed(() => store.state.showUserGuideEnd)
	watch(newVersionEnd, () => {
		store.commit('setShowVideo', videoFlag.value)
	})
	watch(showUserGuideEnd, () => {
		editPasswordStatus.value = true
	})
	return {
		agreement,
		showAgreementDialog,
		tenantName,
		tenantLogo,
		agreementClick,
		showNewVersion,
		editPasswordStatus,
	}
}
